package me.zhengjie.modules.system.service.impl;

import me.zhengjie.base.LazyData;
import me.zhengjie.modules.system.domain.PetBreeds;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.system.service.PetBreedsService;
import me.zhengjie.modules.system.domain.vo.PetBreedsQueryCriteria;
import me.zhengjie.modules.system.mapper.PetBreedsMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author ck
* @date 2025-02-10
**/
@Service
@RequiredArgsConstructor
public class PetBreedsServiceImpl extends ServiceImpl<PetBreedsMapper, PetBreeds> implements PetBreedsService {

    private final PetBreedsMapper petBreedsMapper;

    @Override
    public LazyData<PetBreeds> queryAll(PetBreedsQueryCriteria criteria, Page<Object> page){
        PageResult<PetBreeds> pageResult = PageUtil.toPage(petBreedsMapper.findAll(criteria, page));
        return buildLazyData(page, pageResult);
    }

    private static LazyData<PetBreeds> buildLazyData(Page<Object> page, PageResult<PetBreeds> pageResult) {
        LazyData<PetBreeds> lazyData = new LazyData<>();
        lazyData.setContent(pageResult.getContent());
        lazyData.setTotalElements(pageResult.getTotalElements());
        lazyData.setPageNumber(page.getCurrent());
        lazyData.setPageSize(page.getSize());
        int totalPage = (int) Math.ceil((double) pageResult.getTotalElements() / page.getSize());
        lazyData.setTotalPages(totalPage);
        lazyData.setLast(totalPage == page.getCurrent() + 1); // 最后一页
        lazyData.setFirst(page.getCurrent() == 0); // 第一页
        lazyData.setEmpty(false);
        return lazyData;
    }

    @Override
    public List<PetBreeds> queryAll(PetBreedsQueryCriteria criteria){
        return petBreedsMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(PetBreeds resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PetBreeds resources) {
        PetBreeds petBreeds = getById(resources.getId());
        petBreeds.copy(resources);
        saveOrUpdate(petBreeds);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<PetBreeds> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PetBreeds petBreeds : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("所属类别ID", petBreeds.getCategoryId());
            map.put("品种名称", petBreeds.getName());
            map.put("品种图片URL", petBreeds.getImageUrl());
            map.put("品种描述", petBreeds.getDescription());
            map.put("品种起源", petBreeds.getOrigin());
            map.put("性格特征", petBreeds.getTemperament());
            map.put("预期寿命", petBreeds.getLifeExpectancy());
            map.put("特殊护理需求", petBreeds.getSpecialCareNeeds());
            map.put("常见毛色", petBreeds.getCommonColors());
            map.put("排序号", petBreeds.getSortOrder());
            map.put("是否启用 0-禁用 1-启用", petBreeds.getIsActive());
            map.put("创建时间", petBreeds.getCreatedAt());
            map.put("更新时间", petBreeds.getUpdatedAt());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}